/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.rest;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.domain.Resource;
import me.zhengjie.modules.system.domain.ResourceNum;
import me.zhengjie.modules.system.service.ResourceService;
import me.zhengjie.modules.system.domain.vo.ResourceQueryCriteria;
import lombok.RequiredArgsConstructor;
import java.util.List;

import me.zhengjie.utils.SecurityUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.utils.PageResult;

/**
* @author shitou
* @date 2024-05-04
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "资源管理")
@RequestMapping("/api/resource")
public class ResourceController {

    private final ResourceService resourceService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check('resource:list')")
    public void exportResource(HttpServletResponse response, ResourceQueryCriteria criteria) throws IOException {
        resourceService.download(resourceService.queryAll(criteria), response);
    }



    @GetMapping
    @Log("查询资源")
    @ApiOperation("查询资源")
//    @PreAuthorize("@el.check('resource:list')")
    public ResponseEntity<PageResult<Resource>> queryResource(ResourceQueryCriteria criteria, Page<Object> page){
        return new ResponseEntity<>(resourceService.queryAll(criteria,page),HttpStatus.OK);
    }

    //获取学校资源
    @GetMapping("/querySchoolResource")
    @Log("获取学校资源")
    @ApiOperation("获取学校资源")
    public ResponseEntity<PageResult<Resource>> querySchoolResource(ResourceQueryCriteria criteria, Page<Object> page){
        return new ResponseEntity<>(resourceService.querySchoolResource(criteria,page),HttpStatus.OK);
    }

    //获取学校资源数量
    @GetMapping("/querySchoolResourceNum")
    @Log("获取学校资源数量")
    @ApiOperation("获取学校资源数量")
    public ResponseEntity<ResourceNum> querySchoolResourceNum(ResourceQueryCriteria criteria, Page<Object> page){

        ResourceNum resourceNum = resourceService.querySchoolResourceNum();
        return new ResponseEntity<>(resourceNum,HttpStatus.OK);
    }

    //获取我的资源数量
    @GetMapping("/queryMyResourceNum")
    @Log("获取我的资源数量")
    @ApiOperation("获取我的资源数量")
    public ResponseEntity<ResourceNum> queryMyResourceNum(ResourceQueryCriteria criteria, Page<Object> page){

        ResourceNum resourceNum = new ResourceNum();

        //获取我的资源
        QueryWrapper queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("type",2);
        queryWrapper1.eq("createUserId", SecurityUtils.getCurrentUserId());
//        resourceService.count(queryWrapper1);

        resourceNum.setVideoNum(Math.toIntExact(resourceService.count(queryWrapper1)));


        QueryWrapper queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("type",0);
        queryWrapper2.eq("createUserId", SecurityUtils.getCurrentUserId());
//        resourceService.count(queryWrapper2);
        resourceNum.setImgNum(Math.toIntExact(resourceService.count(queryWrapper2)));

        QueryWrapper queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.eq("type",6);
        queryWrapper3.eq("createUserId", SecurityUtils.getCurrentUserId());

        resourceNum.setFileNum(Math.toIntExact(resourceService.count(queryWrapper3)));


        return new ResponseEntity<>(resourceNum,HttpStatus.OK);
    }

    //获取教师自己创建的资源
    @GetMapping("/queryMyResource")
    @Log("获取教师自己创建的资源")
    @ApiOperation("获取教师自己创建的资源")
    public ResponseEntity<PageResult<Resource>> queryMyResource(ResourceQueryCriteria criteria, Page<Object> page){
        return new ResponseEntity<>(resourceService.queryMyResource(criteria,page),HttpStatus.OK);
    }

    //获取公开的资源
    @GetMapping("/querySchoolPublicResource")
    @Log("获取公开的资源")
    @ApiOperation("获取公开的资源")
    public ResponseEntity<PageResult<Resource>> querySchoolPublicResource(ResourceQueryCriteria criteria, Page<Object> page){
        return new ResponseEntity<>(resourceService.querySchoolPublicResource(criteria,page),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增资源")
    @ApiOperation("新增资源")
//    @PreAuthorize("@el.check('resource:add')")
    public ResponseEntity<Object> createResource(@Validated @RequestBody Resource resources){
        resourceService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改资源")
    @ApiOperation("修改资源")
//    @PreAuthorize("@el.check('resource:edit')")
    public ResponseEntity<Object> updateResource(@Validated @RequestBody Resource resources){
        resourceService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除资源")
    @ApiOperation("删除资源")
//    @PreAuthorize("@el.check('resource:del')")
    public ResponseEntity<Object> deleteResource(@RequestBody List<String> ids) {
        resourceService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}